<template>
  <div class="root">
    <!-- 未登录头部 -->
    <div class="My_title" v-if="!user">
      <img src="http://liufusong.top:8080/img/profile/bg.png" class="My_bg">
      <div class="My_info">
        <div class="My_myIcon">
          <van-image
            width="100%"
            src="http://liufusong.top:8080/img/profile/avatar.png"
          />
        </div>
        <div class="My_user">
          <div class="My_name">游客</div>
          <div class="My_btn">
           <van-button type="default" to="/login" size="small" color="#21b97a">去登录</van-button>
          </div>
        </div>
      </div>
    </div>
    <!-- /未登录头部 -->

    <!-- 已登录头部 -->
    <div class="My_title" v-else>
      <img src="http://liufusong.top:8080/img/avatar.png" class="My_bg">
      <div class="My_info">
        <div class="My_myIcon">
          <van-image
            width="100%"
            :src="'http://liufusong.top:8080'+userInfo.avatar"
          />
        </div>
        <div class="My_user">
          <div class="My_name">{{userInfo.nickname}}</div>
          <div class="My_btn2">
           <van-button type="default" size="small" color="#21b97a" @click="onLogout">退出</van-button>
          </div>
          <div class="My_edit">
            <span>编辑个人资料</span>
            <van-icon name="arrow" color="#999" />
          </div>
        </div>
      </div>
    </div>
    <!-- /已登录头部 -->

    <!-- 导航 -->
    <div class="am-grid">
      <van-grid :column-num="3" :border='false'>
        <van-grid-item icon="star-o" @click="tosc" text="我的收藏" />
        <van-grid-item icon="wap-home-o" @click="tocz" text="我的出租" />
        <van-grid-item icon="clock-o" text="看房记录" />
        <van-grid-item icon="notes-o" text="成为房主" />
        <van-grid-item icon="contact" text="个人资料" />
        <van-grid-item icon="service-o" text="联系我们" />
      </van-grid>
    </div>
    <!-- /导航 -->

    <!-- 底部图片 -->
    <div class="My_ad">
      <img src="http://liufusong.top:8080/img/profile/join.png" alt="">
    </div>
    <!-- /底部图片 -->
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { Dialog } from 'vant'
import { getUser } from '@/api/login'
export default {
  data () {
    return {
      userInfo: {}
    }
  },

  created () {
    if (this.user) {
      this.loadUser()
    }
  },

  methods: {
    onLogout () {
      Dialog.confirm({
        title: '提示',
        message: '是否确认退出'
      })
        .then(() => {
          this.$store.commit('removeUser')
        })
        .catch(() => {
          console.log('不退出')
        })
    },
    async loadUser () {
      try {
        const res = await getUser(this.user.token)
        console.log(res)
        this.userInfo = res
      } catch (err) {
        console.log('获取失败')
      }
    },
    tosc () {
      if (!this.user) {
        this.$router.push('/login')
      } else {
        this.$router.push('/favorate')
      }
    },
    tocz () {
      if (!this.user) {
        this.$router.push('/login')
      } else {
        this.$router.push('/rent')
      }
    }
  },
  computed: {
    ...mapState(['user'])
  }
}
</script>

<style scoped lang='less'>
.root,body,html {
  height: 100%;
  // background-color: aqua;
  .My_title {
    min-height: 300px;
    position: relative;
    .My_bg {
      width: 100%;
    }
    .My_info {
      position: absolute;
      background: #fff;
      width: 85%;
      height: 55%;
      bottom: 0;
      left: 50%;
      transform: translateX(-50%);
      box-shadow: 0 0 10px 3px #ddd;
      margin: 50px auto 0;
      padding: 0 20px;
      text-align: center;
      font-size: 13px;
      .My_myIcon {
        position: relative;
        transform: translateY(-50%);
        border-radius: 50%;
        width: 70px;
        height: 70px;
        border: 5px solid #f5f5f5;
        display: inline-block;
        box-shadow: 0 2px 2px #bdbdbd;
      }
      .My_user {
        padding-top: 15px;
        .My_name {
          margin-top: -30px;
          margin-bottom: 10px;
        }
        .My_btn {
          margin-top: 20px;
        }
        .My_btn2 {
          margin-bottom: 30px;
        }
        .My_edit {
          color: #999;
          font-size: 12px;
          margin-top: 20px;
        }
      }
    }
  }
  .My_ad {
    text-align: center;
    margin-top: 10px;
    img {
      width: 92%;
    }
  }
}
</style>
